<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus"

const handleSuccess = (result) => {
  //响应的结果      //返回的是服务器上面的地址
  console.log(result.data)
  VideoData.value.videoUrl = result.data
  ElMessage.success('success')
}
const handleCovergeSuccess = (result) => {
  VideoData.value.videoCoverUrl = result.data
}

const VideoData = ref({
  username: '',
  videoName: '',
  videoUrl: '',
  videoCoverUrl: ''
})
import {addVideo, getAllVideo} from '@/util/video/video.js'

const addVideos = async () => {
  if (!VideoData.value.username || !VideoData.value.videoName || !VideoData.value.videoUrl) {
    ElMessage.error("出现错误")
    return
  }
  const res = await addVideo(VideoData.value)
  if (res.data.code == 200) {
    ElMessage.success('success')
  }
}
const videos = ref([])
const getAllVideos = async () => {
  const res = await getAllVideo()
  videos.value = res.data.data
}

import router from '@/router/index.js'
</script>

<template>
  <el-form :inline=true :model="VideoData" style="width:500px">
    <el-form-item>
      <el-input v-model="VideoData.username" placeholder="请输入视频的名称"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input v-model="VideoData.videoName" placeholder="请输入视频的名称"></el-input>
    </el-form-item>
    <el-form-item>
      <el-upload
          action="/api/upload"
          name="file"
          auto-upload:true
          :on-success="handleCovergeSuccess">
        <el-button type="primary">请选择上传视频封面</el-button>
      </el-upload>

      <el-upload
          action="/api/upload"
          name="file"
          auto-upload:true
          :on-success="handleSuccess">
        <el-button type="primary">请选择上传的视频</el-button>
      </el-upload>

    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="addVideos">视频投稿</el-button>
    </el-form-item>
  </el-form>

  <!--   查询所有-->
  <el-button type="success" @click="getAllVideos">获取所有的视频</el-button>
  <el-card style="max-width: 480px" v-for="(item) in videos" v-if="videos.length>0">
    <template #header>
      <div class="card-header">
        <span>视频名称{{ item.videoName }}</span>
        <!--         路由跳转过去-->
      </div>
    </template>
    <!--    后台的视频表有会有字段 包括对视频的评论等-->
    <el-image :src=item.videoCoverUrl alt="图片加载失败"
              @click="router.push({path: '/video', query: {id: item.id}})"/>
    <template #footer>视频作者：{{ item.username }}</template>
  </el-card>

</template>

<style scoped>

</style>
